package com.bwei.like.mynewstop.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
<<<<<<< 77331352d74e6afdfbbeaccca4370d33ffbea935
=======
import android.widget.Toast;
>>>>>>> NewsTop

import com.bwei.like.mynewstop.Bean.NewsCategoryRoot;
import com.bwei.like.mynewstop.Bean.OffLineBean;
import com.bwei.like.mynewstop.R;
import com.bwei.like.mynewstop.adapater.OffLineListViewAdapater;
<<<<<<< 77331352d74e6afdfbbeaccca4370d33ffbea935
import com.bwei.like.mynewstop.base.BaseData;
=======
import com.bwei.like.mynewstop.app.MyApplication;
import com.bwei.like.mynewstop.base.BaseData;
import com.bwei.like.mynewstop.utils.CommonUtils;
import com.bwei.like.mynewstop.utils.ImageLoaderUtils;
>>>>>>> NewsTop
import com.bwei.like.mynewstop.utils.LogUtils;
import com.bwei.like.mynewstop.utils.TitileUtils;
import com.bwei.like.mynewstop.utils.URLUtils;
import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.xutils.common.util.LogUtil;

<<<<<<< 77331352d74e6afdfbbeaccca4370d33ffbea935
import java.util.ArrayList;

public class OffLineActivity extends AppCompatActivity implements View.OnClickListener {
=======
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

public class OffLineActivity extends BaseActivity implements View.OnClickListener {
>>>>>>> NewsTop

    private ImageView offLine_back_image;
    private TextView offLine_Load;
    private CheckBox offLine_select_All_cb;
    private ListView listView;
    private ArrayList<OffLineBean> offLineBeanArrayList;

    @Override
<<<<<<< 77331352d74e6afdfbbeaccca4370d33ffbea935
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
=======
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
>>>>>>> NewsTop
        setContentView(R.layout.activity_off_line);
        initView();

        //设置listview适配器
        listView.setAdapter(new OffLineListViewAdapater(offLineBeanArrayList, this, offLine_select_All_cb));
    }

    private void initView() {
        offLine_back_image = (ImageView) findViewById(R.id.offLine_back_Image);
        offLine_Load = (TextView) findViewById(R.id.offLoad_tv);
        offLine_select_All_cb = (CheckBox) findViewById(R.id.select_All_cb);
        listView = (ListView) findViewById(R.id.offLine_Listview);

        //设置监听事件
        offLine_back_image.setOnClickListener(this);
        offLine_Load.setOnClickListener(this);
        //自定义集合
        offLineBeanArrayList = new ArrayList<>();
        for (int i = 0; i < TitileUtils.myChannel.size(); i++) {
            offLineBeanArrayList.add(new OffLineBean(TitileUtils.myChannel.get(i).substring(0, TitileUtils.myChannel.get(i).indexOf(":")), false));
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //图片返回按钮
            case R.id.offLine_back_Image:
                finish();
                overridePendingTransition(R.anim.phone_in, R.anim.cell_regeist_out);
                break;

            //下载按钮监听
            case R.id.offLoad_tv:
                for (int i = 0; i < offLineBeanArrayList.size(); i++) {
                    if (offLineBeanArrayList.get(i).isChecked()) {
                        String category = TitileUtils.myChannel.get(i).substring(TitileUtils.myChannel.get(i).indexOf(":") + 1);
                        getNetData(URLUtils.URL_CATEGROY_PATH + category);
                    }
                }
                break;
        }
    }


    public void getNetData(String requestPath) {
        BaseData baseData = new BaseData() {
            @Override
<<<<<<< 77331352d74e6afdfbbeaccca4370d33ffbea935
            public void setResultData(String path) {
=======
            public void setResultData(final String path) {
>>>>>>> NewsTop
                //解析Gson
                if (!TextUtils.isEmpty(path)) {
                    Gson gson = new Gson();
                    NewsCategoryRoot newsCategoryRoot = gson.fromJson(path, NewsCategoryRoot.class);
<<<<<<< 77331352d74e6afdfbbeaccca4370d33ffbea935
                    //.getInstance().displayImage(newsCategoryRoot.getData().get(position).getLarge_image_list().get(0).url, viewHolder.iv_news_pic, initOptions);
                    LogUtils.i("111111111111111TAG", newsCategoryRoot.getData().get(4).getImage_list().get(0).url);

//                    for (int i = 0; i < newsCategoryRoot.getData().size(); i++) {
//                        if (newsCategoryRoot.getData().get(i).isHas_video()) {
//                            ImageLoader.getInstance().loadImageSync(newsCategoryRoot.getData().get(i).getLarge_image_list().get(0).url);
//                        } else if (newsCategoryRoot.getData().get(i).getLarge_image_list() != null && newsCategoryRoot.getData().get(i).getLarge_image_list().size() > 0) {
//                            ImageLoader.getInstance().loadImageSync(newsCategoryRoot.getData().get(i).getLarge_image_list().get(0).url);
//                        } else if (newsCategoryRoot.getData().get(i).getImage_list() != null && newsCategoryRoot.getData().get(i).getImage_list().size() > 1) {
//                            LogUtils.i("00000000000TAG", newsCategoryRoot.getData().get(i).getImage_list().get(0).url);
//                            ImageLoader.getInstance().loadImageSync(newsCategoryRoot.getData().get(i).getImage_list().get(0).url);
//                            ImageLoader.getInstance().loadImageSync(newsCategoryRoot.getData().get(i).getImage_list().get(1).url);
//                          //  ImageLoader.getInstance().loadImageSync(newsCategoryRoot.getData().get(i).getImage_list().get(2).url);
//                        }
//                    }

=======
                    for (int i = 0; i < newsCategoryRoot.getData().size(); i++) {
                        final NewsCategoryRoot.DataBean dataBean = newsCategoryRoot.getData().get(i);
                        if (dataBean.isHas_video()) {
                            if (dataBean.getLarge_image_list().get(0) != null) {
                                CommonUtils.executeRunnable(new Runnable() {
                                    @Override
                                    public void run() {
                                        try {
                                            ImageLoader.getInstance().loadImageSync(dataBean.getLarge_image_list().get(0).url, ImageLoaderUtils.initOptions());
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }
                                    }
                                });
                            }
                        }

                        if (!dataBean.isHas_image()) {

                        }

                        if (dataBean.getLarge_image_list() != null && dataBean.getLarge_image_list().size() > 0) {
                            CommonUtils.executeRunnable(new Runnable() {
                                @Override
                                public void run() {
                                    try {
                                        ImageLoader.getInstance().loadImageSync(dataBean.getLarge_image_list().get(0).url, ImageLoaderUtils.initOptions());
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                            });
                        }

                        if (dataBean.getImage_list() != null && dataBean.getImage_list().size() > 1) {
                            CommonUtils.executeRunnable(new Runnable() {
                                @Override
                                public void run() {
                                    try {
                                        ImageLoader.getInstance().loadImageSync(dataBean.getImage_list().get(2).url, ImageLoaderUtils.initOptions());
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                            });

                            CommonUtils.executeRunnable(new Runnable() {
                                @Override
                                public void run() {
                                    try {
                                        ImageLoader.getInstance().loadImageSync(dataBean.getImage_list().get(1).url, ImageLoaderUtils.initOptions());
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                            });


                            CommonUtils.executeRunnable(new Runnable() {
                                @Override
                                public void run() {
                                    try {
                                        ImageLoader.getInstance().loadImageSync(dataBean.getImage_list().get(0).url, ImageLoaderUtils.initOptions());
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                            });
                        }

                    }
>>>>>>> NewsTop
                }
            }

            @Override
            protected void setFailResult(int error_Net) {
<<<<<<< 77331352d74e6afdfbbeaccca4370d33ffbea935

=======
                Toast.makeText(OffLineActivity.this, "网络出错，请检查网络重新下载！！", Toast.LENGTH_SHORT).show();
>>>>>>> NewsTop
            }
        };

        baseData.getData(requestPath, BaseData.NOTIME);
    }
}
